# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name： zabbix_alarm_format
Description :
Author : 'li'
date： 1/15/2024
-------------------------------------------------
Change Activity:
1/15/2024:
-------------------------------------------------
"""
import time

import ll
from projects.monitoring.alarm_manager.utils.format.alarm_format import AlarmFormat
from projects.monitoring.common.do.alarm import Alarm


class BranchDedicatedLineAlarmFormat(AlarmFormat):
    def __init__(
            self,
            subscribe_alarm_info,
            time_format="%Y-%m-%d %H:%M:%S"
    ):
        super().__init__(subscribe_alarm_info, time_format)

    def _format_alarm(self, alarm):
        """
        This function formats the zabbix alarm.
        Args:
            alarm:

        Returns:

        """
        alarm_obj = Alarm(
            timestamp=time.time(),
            ip=alarm['ip'],
            hostname=alarm['branch'],
            severity='warning',
            alarm_type='alarm',
            alarm_info=f"专线{alarm['branch']}-{alarm['ip']} 连接异常,路由器IP:{alarm['router_ip']}",
            recovery=False,
            recovery_time=None,
            manager_ip=alarm['router_ip'],
            manager_port=None,
            alarm_source='branch_dedicated_line'
        )
        return alarm_obj

    @staticmethod
    def _parse_recovery_alarm(alarm):
        """
        This function parses the recovery alarm.
        Args:
            alarm:

        Returns:

        """
        recovery_time = ll.TimeUtil.date_stamp_to_datetime(
            alarm['r_clock'],
            format_str="%Y-%m-%d %H:%M:%S"
        )
        alarm_type = 'recovery'
        recovery = True
        return recovery_time, alarm_type, recovery
